package com.jrx.anytxn.transaction.job.chargeoffProcess.config;

import com.jrx.anytxn.transaction.bean.ChargeoffBean;
import com.jrx.anytxn.transaction.config.AnytxnBatchProperties;
import com.jrx.anytxn.transaction.constant.TransBizConstant;
import com.jrx.anytxn.transaction.entity.TlChargeoffInfo;
import com.jrx.anytxn.transaction.job.chargeoffProcess.step.ChargeoffProcessor;
import com.jrx.anytxn.transaction.job.chargeoffProcess.step.ChargeoffWriter;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.batch.MyBatisCursorItemReader;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.item.support.SynchronizedItemStreamReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.transaction.PlatformTransactionManager;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 核销处理
 */
@Configuration
@EnableConfigurationProperties(AnytxnBatchProperties.class)
public class ChargeoffConfiguration {
    @Resource(name = "bizSqlSessionFactory")
    SqlSessionFactory sqlSessionFactory;
    @Autowired
    public JobBuilderFactory jobBuilderFactory;
    @Autowired
    public StepBuilderFactory stepBuilderFactory;
    @Autowired
    private AnytxnBatchProperties anytxnBatchProperties;
    @Autowired
    private ChargeoffProcessor chargeoffProcessor;
    @Autowired
    private ChargeoffWriter chargeoffWriter;
    @Autowired
    private PlatformTransactionManager platformTransactionManager;


    @Bean
    public Job chargeoffJob(){
        return jobBuilderFactory.get("chargeoffJob")
                .start(chargeoffStep())//批扣第一步 记录还款流水
                .build();
    }

    @Bean
    public Step chargeoffStep(){
        return stepBuilderFactory.get("chargeoffStep")
                .<TlChargeoffInfo, ChargeoffBean>chunk(anytxnBatchProperties.getChargeoffConfig().getChunkLimit())
                //表示一批处理100
                // 条记录，数据是逐条发送给processor处理的，处理100条记录后，统一返回给writer进行保存处理
                .reader(chargeoffReader())
                .processor(chargeoffProcessor)
                .writer(chargeoffWriter)
                .transactionManager(platformTransactionManager)
                .taskExecutor(new SimpleAsyncTaskExecutor())
                .throttleLimit(anytxnBatchProperties.getChargeoffConfig().getThrottleLimit())//多线程*/
                .build();
    }

    @Bean
    @StepScope
    public SynchronizedItemStreamReader<TlChargeoffInfo> chargeoffReader() {
        MyBatisCursorItemReader<TlChargeoffInfo> itemReader = new MyBatisCursorItemReader<>();
        itemReader.setSaveState(false);
        itemReader.setSqlSessionFactory(sqlSessionFactory);
        //查询方法
        itemReader.setQueryId("com.jrx.anytxn.transaction.mapper.ext.ExtTlChargeoffInfoMapper.selectByResultForBatch");
        Map<String,Object> map = new HashMap<>();
        //查询参数设置
        map.put("result", TransBizConstant.CHARGEOFF_STATUS_D);
        itemReader.setParameterValues(map);
        //同步reader   线程安全
        SynchronizedItemStreamReader<TlChargeoffInfo> synchronizedItemStreamReader = new SynchronizedItemStreamReader<>();
        synchronizedItemStreamReader.setDelegate(itemReader);
        return synchronizedItemStreamReader;
    }
}
